package com.ruoyi.common.utils;

import com.github.pagehelper.util.StringUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class ImgUntil {


    /**
     * 写有间距的文字
     * @param str 文字内容
     * @param x 开始x位置
     * @param y 开始y位置
     * @param rate 间距（x倍）
     * @param g Graphics对象
     */
    public static void myDrawString(String str, int x, int y, double rate, Graphics g) {
        String tempStr;
        int orgStringWight = g.getFontMetrics().stringWidth(str);
        int orgStringLength = str.length();
        int tempx = x;
        int tempy = y;
        while (str.length() > 0) {
            tempStr = str.substring(0, 1);
            str = str.substring(1);
            g.drawString(tempStr, tempx, tempy);
            tempx = (int) (tempx + (double) orgStringWight / (double) orgStringLength * rate);
        }
    }
    public static int drawStringWithFontStyleLineFeedCommon(Graphics g, String strContent, int maxWdith, int loc_X, int loc_Y, Font font,Integer initLeft,Integer lineHeight) {
        if (Objects.isNull(lineHeight)){
            lineHeight = 20;
        }
        g.setFont(font);
        //获取字符串 字符的总宽度
        int strWidth = getStringLength(g, strContent);
        //每一行字符串宽度
        // System.out.println("每行字符宽度:"+rowWidth);
        //获取字符高度
        int strHeight = getStringHeight(g,lineHeight);
        //字符串总个数
        //  System.out.println("字符串总个数:"+strContent.length());
        int line = 0;
        int rowWidth = maxWdith ;
        if (strWidth > rowWidth) {
            char[] strContentArr = strContent.toCharArray();
            int count = 0;
            int conut_value = 0;
            int charWidth = 0;
            for (int j = 0; j < strContentArr.length; j++) {
                strWidth = strWidth + loc_X;
                if (conut_value >= rowWidth) {
                    conut_value = 0;
                    g.drawString(strContent.substring(count, j), loc_X, loc_Y + strHeight * line);
                    count = j;
                    line++;
                    if (Objects.nonNull(initLeft)){
                        loc_X = initLeft;
                    }
                } else {
                    if (j == strContentArr.length - 1) {
                        g.drawString(strContent.substring(count, j), loc_X, loc_Y + strHeight * line);
                        line++;
                    } else {
                        charWidth = g.getFontMetrics().charWidth(strContentArr[j]);
                        conut_value = charWidth + conut_value;
                    }
                }

            }

        } else {
            //直接绘制
            g.drawString(strContent, loc_X, loc_Y);
            return strHeight;
        }
        return strHeight*line;
    }

    /**
     * 根据指定宽度自动换行
     *
     * @param g
     * @param maxWdith
     * @param strContent
     * @param loc_X
     * @param loc_Y
     * @param font
     */
    public static int drawStringWithFontStyleLineFeed(Graphics g, String strContent, int maxWdith, int loc_X, int loc_Y, Font font,Integer initLeft,Integer lineHeight) {
        if (Objects.isNull(lineHeight)){
            lineHeight = 20;
        }
        g.setFont(font);
        //获取字符串 字符的总宽度
        int strWidth = getStringLength(g, strContent);
        //每一行字符串宽度
        // System.out.println("每行字符宽度:"+rowWidth);
        //获取字符高度
        int strHeight = getStringHeight(g,lineHeight);
        //字符串总个数
        //  System.out.println("字符串总个数:"+strContent.length());
        int line = 0;
        int rowWidth = maxWdith - loc_X + 100;
        if (strWidth > rowWidth) {
            char[] strContentArr = strContent.toCharArray();
            int count = 0;
            int conut_value = 0;
            int charWidth = 0;
            for (int j = 0; j < strContentArr.length; j++) {
                strWidth = strWidth + loc_X;
                if (conut_value >= rowWidth) {
                    conut_value = 0;
                    g.drawString(strContent.substring(count, j), loc_X, loc_Y + strHeight * line);
                    count = j;
                    line++;
                    if (Objects.nonNull(initLeft)){
                        loc_X = initLeft;
                    }
                    rowWidth = maxWdith - 100;
                } else {
                    if (j == strContentArr.length - 1) {
                        String substring = strContent.substring(count, j + 1);
                        System.out.println(substring);
                        g.drawString(strContent.substring(count, j + 1), loc_X, loc_Y + strHeight * line);
                        line++;
                    } else {
                        charWidth = g.getFontMetrics().charWidth(strContentArr[j]);
                        conut_value = charWidth + conut_value;
                    }
                }

            }

        } else {
            //直接绘制
            g.drawString(strContent, loc_X, loc_Y);
            return strHeight;
        }
        return strHeight*line;
    }
    /**
     * 根据指定宽度自动换行
     *
     * @param g
     * @param maxWdith
     * @param strContent
     * @param loc_X
     * @param loc_Y
     * @param font
     */
    public static int drawStringWithFontStyleLineFeedTitle(Graphics g, String strContent, int maxWdith, int loc_X, int loc_Y, Font font,Integer initLeft,Integer lineHeight) {
        if (Objects.isNull(lineHeight)){
            lineHeight = 20;
        }
        g.setFont(font);
        //获取字符串 字符的总宽度
        int strWidth = getStringLength(g, strContent);
        //每一行字符串宽度
        // System.out.println("每行字符宽度:"+rowWidth);
        //获取字符高度
        int strHeight = getStringHeight(g,lineHeight);
        //字符串总个数
        //  System.out.println("字符串总个数:"+strContent.length());

        String[] s = strContent.split(" ");
        List<String> gameSkins = Arrays.asList(s);
        if (gameSkins.size() > 32){
            gameSkins.subList(0,31);
        }
        int i = 0;
        int line = 1;
        for (String gameSkin : gameSkins) {

            if (StringUtil.isNotEmpty(gameSkin)){
                if(line == 1){
                    g.drawString(gameSkin, i*700+200, 1200 + line*100);
                }else {
                    g.drawString(gameSkin, (i-1)*700+200, 1200 + line*100);
                }

                if (i % 4 == 0 && i != 0){
                    line ++;
                    i = 0;
                }
                i++;
            }

        }
        return strHeight;

//        int rowWidth = maxWdith - loc_X + 100;
//        if (strWidth > rowWidth) {
//            char[] strContentArr = strContent.toCharArray();
//            int count = 0;
//            int conut_value = 0;
//            int charWidth = 0;
//            for (int j = 0; j < strContentArr.length; j++) {
//                strWidth = strWidth + loc_X;
//                if (conut_value >= rowWidth) {
//                    conut_value = 0;
//                    g.drawString(strContent.substring(count, j), loc_X, loc_Y + strHeight * line);
//                    count = j;
//                    line++;
//                    if (Objects.nonNull(initLeft)){
//                        loc_X = initLeft;
//                    }
//                    rowWidth = maxWdith - 100;
//                } else {
//                    if (j == strContentArr.length - 1) {
//                        String substring = strContent.substring(count, j + 1);
//                        System.out.println(substring);
//                        g.drawString(strContent.substring(count, j + 1), loc_X, loc_Y + strHeight * line);
//                        line++;
//                    } else {
//                        charWidth = g.getFontMetrics().charWidth(strContentArr[j]);
//                        conut_value = charWidth + conut_value;
//                    }
//                }
//
//            }
//
//        } else {
//            //直接绘制
//            g.drawString(strContent, loc_X, loc_Y);
//            return strHeight;
//        }
    }

    private static int getStringLength(Graphics g, String str) {
        char[] strcha = str.toCharArray();
        int strWidth = g.getFontMetrics().charsWidth(strcha, 0, str.length());
        System.out.println("字符总宽度:" + strWidth);
        return strWidth;
    }


    private static int getStringHeight(Graphics g, Integer lineHeight) {
        int height = g.getFontMetrics().getHeight()+lineHeight;
        //System.out.println("字符高度:"+height);
        return height;
    }

    /**
     * 将BufferedImage转换为byte[]
     * @param image
     * @return
     */
    public static byte[] bufferedImageToByteArray(BufferedImage image) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        return os.toByteArray();
    }

    /**
     * 合并任数量的图片成一张图片
     * <p>
     * true代表水平合并，fasle代表垂直合并
     *
     * @param imgs 待合并的图片数组
     * @return
     * @throws IOException
     */
    public static BufferedImage mergeImage(List<BufferedImage> imgs) throws IOException {
        // 生成新图片
        BufferedImage destImage = null;
        // 计算新图片的长和高
        int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
        // 获取总长、总宽、最长、最宽
        for (int i = 0; i < imgs.size(); i++) {
            BufferedImage img = imgs.get(i);

            if (i % 4 == 0) {
                allh += img.getHeight();
            } else {
                allw += img.getWidth();
            }
//            if (img.getWidth() > allwMax) {
//                allwMax = img.getWidth();
//            }
//            if (img.getHeight() > allhMax) {
//                allhMax = img.getHeight();
//            }
        }

        destImage = new BufferedImage(5000, 5000, BufferedImage.TYPE_INT_RGB);
        // 创建新图片
//        if (isHorizontal) {
//            destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
//        } else {
//            destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
//        }
        // 合并所有子图片到新图片
        int wx = 0, wy = 0;
        for (int i = 1; i < imgs.size()+1; i++) {
            BufferedImage img = imgs.get(i-1);
            int w1 = img.getWidth();
            int h1 = img.getHeight();
            // 从图片中读取RGB
            int[] ImageArrayOne = new int[w1 * h1];
            ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
//            if (isHorizontal) { // 水平方向合并
//                destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
//            } else { // 垂直方向合并
//                destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
//            }
            if (i % 4 != 0) {
                destImage.setRGB(wx, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
                wx += w1;
            }else {
                destImage.setRGB(wx, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
                wy += h1;
                wx = 0;
            }

        }
        return destImage;
    }

}
